package com.zhipu.balance.utils.data

import android.text.TextUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.zhipu.balance.common.Constants
import java.text.DecimalFormat

object DisposeUtils {

    var mDecimalFormat = DecimalFormat("0.00");


    /**
     * 天平回调
     */
    fun balanceCorrection(event: ByteArray):String{
        return if (event[1].toInt() == 0) {//不稳定数据
            byteConvertInto(event)
        } else if (event[1].toInt() == 40) {//秤重大於滿載
            byteConvertInto(event)
        } else if (event[1].toInt() == 41) {//秤重大於滿載
            byteConvertInto(event)
        } else if (event[1].toInt() == 64) {//秤重大於滿載
            byteConvertInto(event)
        } else if (event[1].toInt() == 65) {//秤重大於滿載
            byteConvertInto(event)
        } else if (event[1].toInt() == 68) {//秤重大於滿載
            byteConvertInto(event)
        } else if (event[1].toInt() == 69) {//秤重大於滿載
            byteConvertInto(event)
        } else{
            byteConvertInto(event)
        }
    }


    /**
     * 数据
     */
    fun byteConvertInto(dataByteArray : ByteArray) : String{
        if(dataByteArray.size != 11){
            return "0.00";
        }
        /**
         * 正负
         */
        var plusMinus : String = if(dataByteArray[2].toInt() == 45){
            "-"
        }else{
            ""
        }
        /**
         * 千
         */
        val hexThousand = dataByteArray.copyOfRange(3,4)
        var thousand = String(hexThousand, Charsets.UTF_8)
        if(thousand == " "){
            thousand = ""
        }
        /**
         *  百
         */
        val hexHundred = dataByteArray.copyOfRange(4,5)
        var hundred = String(hexHundred, Charsets.UTF_8)
        if(hundred == " "){
            hundred = ""
        }
        /**
         * 十
         */
        val hexTen = dataByteArray.copyOfRange(5,6)
        var ten = String(hexTen, Charsets.UTF_8)
        if(ten == " "){
            ten = ""
        }
        /**
         * 个
         */
        val hexOne = dataByteArray.copyOfRange(6,7)
        var one = String(hexOne, Charsets.UTF_8)
        if(one == " "){
            one = "0"
        }
        /**
         * 小数点第一位
         */

        var decimalPointFirst = String(dataByteArray.copyOfRange(7,8), Charsets.UTF_8)
        if(decimalPointFirst == " "){
            decimalPointFirst = "0"
        }

        /**
         * 小数点第二位
         */
        var decimalPointSecond = String(dataByteArray.copyOfRange(8,9), Charsets.UTF_8)
        if(decimalPointSecond == " "){
            decimalPointSecond = "0"
        }
        return if(Constants.TWO_PERHAPS_THREE){
            "${plusMinus}${thousand}${hundred}${ten}${one}${decimalPointFirst}.${decimalPointSecond}"
        }else{
            "${plusMinus}${thousand}${hundred}${ten}${one}.${decimalPointFirst}${decimalPointSecond}"
        }


    }

}